package com.cloud.sky.controller;

import com.cloud.sky.component.TingYuClient;
import com.cloud.sky.entity.bo.PageBO;
import com.cloud.sky.entity.bo.ResultBO;
import com.cloud.sky.entity.common.Result;
import com.cloud.sky.entity.common.ResultCode;
import com.cloud.sky.entity.dto.BoxListQueryDTO;
import com.cloud.sky.entity.dto.RouteBindRequestDTO;
import com.cloud.sky.entity.dto.RouteListQueryDTO;
import com.cloud.sky.entity.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 小节点相关
 */
@RestController
public class SmallNodeController {

    @Autowired
    private TingYuClient tingYuClient;

    @RequestMapping("/box/monitor")
    public Result<SmallNodeMonitorInfoVO> getSmallNodeMonitor(@RequestParam(required = false) String uuid,
                                                              @RequestParam(required = false) String deviceId,
                                                              String startTime, String endTime) {
        if (StringUtils.isEmpty(uuid) && StringUtils.isEmpty(deviceId)) {
            return Result.failure(ResultCode.validationFail);
        }
        return Result.success(tingYuClient.getSmallNodeMonitor(uuid, deviceId, startTime, endTime));
    }

    @RequestMapping("/box/detail")
    public Result<SmallNodeDetailVO> getSmallNodeDetail(@RequestParam(required = false) String uuid,
                                                        @RequestParam(required = false) String deviceId) {
        if (StringUtils.isEmpty(uuid) && StringUtils.isEmpty(deviceId)) {
            return Result.failure(ResultCode.validationFail);
        }
        return Result.success(tingYuClient.getSmallNodeDetail(uuid, deviceId));
    }

    @RequestMapping("/box/daily/list")
    public Result<List<SmallNodeIncomeVO>> getSmallNodeIncomeList(@RequestParam(required = false) String subId,
                                                                  @RequestParam(required = false) String uuid,
                                                                  @RequestParam(required = false) String modelType,
                                                                  String startDate, String endDate) {
        return Result.success(tingYuClient.getSmallNodeIncomeList(subId, uuid, modelType, startDate, endDate));
    }

    @RequestMapping("/box/daily/{date}")
    public Result<List<SmallDailyIncomeDetailVO>> getSmallNodeDailyIncomeDetail(@RequestParam(required = false) String uuid,
                                                                                @RequestParam(required = false) String deviceId,
                                                                                @RequestParam(required = false) String subId, @PathVariable("date") String date) {
        if (StringUtils.isEmpty(uuid) && StringUtils.isEmpty(deviceId)) {
            return Result.failure(ResultCode.validationFail);
        }
        return Result.success(tingYuClient.getSmallNodeDailyIncomeDetail(uuid, deviceId, subId, date));
    }

    /**
     * 查看盒子列表
     *
     * @param dto
     * @return
     */
    @RequestMapping("/box/all/list")
    public Result<PageBO<BoxInfoVO>> getBoxList(BoxListQueryDTO dto) {
        if (ObjectUtils.isEmpty(dto)) {
            return Result.failure(ResultCode.validationFail);
        }
        return Result.success(tingYuClient.getBoxList(dto));
    }

    @RequestMapping("/box/daily/forfeitures")
    public Result<List<BoxForfeituresInfoVO>> getForfeituresBoxList(@RequestParam(required = false) String uuid,
                                                                    @RequestParam(required = false) String deviceId,
                                                                    @RequestParam(required = false) String subId,
                                                                    String startDate, String endDate) {
        if (StringUtils.isEmpty(uuid) && StringUtils.isEmpty(deviceId)) {
            return Result.failure(ResultCode.validationFail);
        }
        return Result.success(tingYuClient.getForfeituresBoxList(uuid, deviceId, subId, startDate, endDate));
    }

    @RequestMapping("/box/remove")
    public Result removeBox(String uuid, @RequestParam(required = false) String deviceId) {
        ResultBO resultBO = tingYuClient.removeBox(uuid, deviceId);
        if (resultBO.isSuccess()) {
            return Result.success(null);
        }
        return Result.failure(ResultCode.failure);
    }

    @RequestMapping("/router/bind")
    public Result<RouteBindInfoVO> routeBind(RouteBindRequestDTO routeBindRequestDTO) {
        return Result.success(tingYuClient.routeBind(routeBindRequestDTO));
    }

    @RequestMapping("/router/wait_distribution/list")
    public Result<PageBO<RouteWaitConnectInfoVO>> getRouteWaitConnectNetList(RouteListQueryDTO routeListQueryDTO) {
        return Result.success(tingYuClient.getRouteWaitConnectNetList(routeListQueryDTO));
    }

    /**
     * 路由器列表
     *
     * @param routeListQueryDTO
     * @return
     */
    @RequestMapping("/router/all/list")
    public Result<PageBO<RouteConnectedInfoVO>> getRouteConnectedNetList(RouteListQueryDTO routeListQueryDTO) {
        return Result.success(tingYuClient.getRouteConnectedNetList(routeListQueryDTO));
    }
}
